package com.tledu.car.service.impl;

import com.tledu.car.domain.CarList;
import com.tledu.car.mapper.CarListMapper;
import com.tledu.car.service.ICarListService;
import com.tledu.carLog.domain.CarLog;
import com.tledu.carLog.mapper.CarLogMapper;
import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@Service
public class CarListServiceImpl implements ICarListService {
    @Resource
    private CarListMapper carListMapper;
    @Resource
    private CarLogMapper carLogMapper;

    /**
     * 查询车辆列表
     *
     * @param id 车辆列表主键
     * @return 车辆列表
     */
    @Override
    public CarList selectCarListById(Long id) {
        return carListMapper.selectCarListById(id);
    }

    /**
     * 查询车辆列表
     *
     * @param licence 车辆列表车牌号
     * @return 车辆列表
     */
    @Override
    public CarList selectCarListByLicence(String licence) {
        return carListMapper.selectCarListByLicence(licence);
    }

    /**
     * 查询车辆列表列表
     *
     * @param carList 车辆列表
     * @return 车辆列表
     */
    @Override
    public List<CarList> selectCarListList(CarList carList) {
        return carListMapper.selectCarListList(carList);
    }

    /**
     * 新增车辆列表
     *
     * @param carList 车辆列表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCarList(CarList carList) {
        //插入车辆信息
        carList.setCreateTime(DateUtils.getNowDate());
        carList.setCreateUser(SecurityUtils.getUserId());
        int i = carListMapper.insertCarList(carList);
        //同步插入操作日志
        CarLog carLog = new CarLog();
        carLog.setCarId(carList.getId());
        carLog.setCreateTime(DateUtils.getNowDate());
        carLog.setCreateUser(SecurityUtils.getUserId());
        carLog.setOperateType(0L);
        carLogMapper.insertCarLog(carLog);
        return i;
    }

    /**
     * 修改车辆列表
     *
     * @param carList 车辆列表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCarList(CarList carList) {
        carList.setUpdateTime(DateUtils.getNowDate());
        carList.setUpdateUser(SecurityUtils.getUserId());
        CarLog carLog = new CarLog();
        carLog.setOperateType(2L);
        carLog.setCreateTime(DateUtils.getNowDate());
        carLog.setCreateUser(SecurityUtils.getUserId());
        carLog.setCarId(carList.getId());
        carLogMapper.insertCarLog(carLog);
        return carListMapper.updateCarList(carList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCarList1(CarList carList) {
        carList.setUpdateTime(DateUtils.getNowDate());
        carList.setUpdateUser(SecurityUtils.getUserId());
        CarLog carLog = new CarLog();
        carLog.setOperateType(2L);
        carLog.setCreateTime(DateUtils.getNowDate());
        carLog.setCreateUser(SecurityUtils.getUserId());
        carLog.setCarId(carList.getId());
        carLogMapper.insertCarLog(carLog);
        return carListMapper.updateCarList1(carList);
    }

    /**
     * 隐藏车辆列表
     *
     * @param carList 车辆列表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeCarList(long id) {
        CarLog carLog = new CarLog();
        carLog.setOperateType(1L);
        carLog.setCreateTime(DateUtils.getNowDate());
        carLog.setCreateUser(SecurityUtils.getUserId());
        carLog.setCarId(id);
        carLogMapper.insertCarLog(carLog);
        return carListMapper.removeCarList(id);
    }

    /**
     * 批量删除车辆列表
     *
     * @param ids 需要删除的车辆列表主键
     * @return 结果
     */
    @Override
    public int deleteCarListByIds(Long[] ids) {
        return carListMapper.deleteCarListByIds(ids);
    }

    /**
     * 删除车辆列表信息
     *
     * @param id 车辆列表主键
     * @return 结果
     */
    @Override
    public int deleteCarListById(Long id) {
        return carListMapper.deleteCarListById(id);
    }

    @Override
    public int updateCarList2(Long id) {
        return carListMapper.updateCarList2(id);
    }
}
